home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / byacc 1.8.2 / pascal.y < prev    next >
Encoding:
Lex Description  |  1993-02-03  |  3.6 KB  |  169 lines  |  [TEXT/R*ch]

  1. %{
  2.   /* C stuff here */
  3. #ifndef __GNUC__
  4. #include "alloca.h"
  5. #endif
  6. #include "globals.h"
  7. int yyparse(void);
  8. extern void yyerror(const char *msg);
  9. extern int yylex(void);
  10.  
  11. %}
  12.  
  13. %token T_AND T_ARRAY T_BEGIN T_BOOLEAN T_CHAR T_DIV T_DO
  14. %token T_ELSE T_END T_FALSE T_FUNCTION T_IF
  15. %token T_INTEGER T_MOD T_NOT T_OF T_OR T_PROCEDURE
  16. %token T_PROGRAM T_READ T_REAL T_THEN
  17. %token T_TRUE T_VAR T_WHILE T_WRITE
  18.  
  19. %token T_LTEQ T_NOTEQ T_LT T_GTEQ T_GT T_EQ
  20. %token T_ASSIGNOP T_DOTDOT
  21.  
  22. %token T_DIGITS T_NUM T_ID
  23.  
  24. %nonassoc T_THEN       /* to eliminate shift/reduce error */
  25. %nonassoc T_ELSE
  26.  
  27. %start program
  28.  
  29. %%
  30.  
  31. program: T_PROGRAM T_ID '(' id_list ')' ';'
  32.          declns subprog_declns compound_stmt '.'
  33.         ;
  34.          
  35.  
  36. id_list:  T_ID
  37.         | id_list ',' T_ID
  38.         | error ','
  39.         ;
  40.  
  41. declns:   declns T_VAR id_list ':' type ';'
  42.         | /* empty */
  43.         ;
  44.  
  45. type:     std_type
  46.         | array_prefix T_DOTDOT T_DIGITS ']' T_OF std_type
  47.         | array_prefix    ','   { yyerror("use \"..\" instead of a comma");}
  48.           T_DIGITS ']' T_OF std_type
  49.         ;
  50.  
  51. array_prefix: T_ARRAY '[' T_DIGITS
  52.         ;
  53.  
  54. std_type: T_INTEGER | T_REAL | T_BOOLEAN | T_CHAR
  55.         | T_ID { yyerror("illegal data type"); }
  56.         ;
  57.  
  58. subprog_declns: subprog_declns subprog_decln ';'
  59.         | /* empty */
  60.         ;
  61.  
  62. subprog_decln: subprog_head declns subprog_declns compound_stmt
  63.         ;
  64.  
  65. subprog_head: T_FUNCTION T_ID arguments ':' std_type ';'
  66.         |  T_PROCEDURE T_ID arguments ';'
  67.         ;
  68.  
  69. arguments: '(' parameter_list ')'
  70.         | /* empty */
  71.         ;
  72.  
  73. parameter_list: id_list ':' type
  74.         | parameter_list ';' id_list ':' type
  75.         | error ';'
  76.         ;
  77.  
  78. compound_stmt: T_BEGIN stmt_list T_END
  79.         ;
  80.  
  81. stmt_list: stmt
  82.         |  stmt_list ';' stmt
  83.         ;
  84.  
  85. stmt:     compound_stmt
  86.         | T_WHILE expression T_DO stmt
  87.         | T_IF expression T_THEN stmt
  88.         | T_IF expression T_THEN stmt T_ELSE stmt
  89.         | T_READ '(' expression_list ')'
  90.         | T_WRITE '(' expression_list ')'
  91.         | T_ID assignop expression            /* expanded "variable" rule */
  92.         | T_ID '[' expression ']' assignop expression
  93.         | T_ID                              /* expanded procedure_stmt rule */
  94.         | T_ID '(' expression ')'
  95.         | T_ID '(' expression ')'
  96.           assignop { yyerror("can't assign a subroutine call a value");}
  97.           expression
  98.         | /* empty */
  99.         ;
  100.  
  101. assignop: T_ASSIGNOP
  102.         | T_EQ {yyerror("assignment operator required (:=)");}
  103.         ;
  104.  
  105. /*  These rules became useless when above stmt rule was expanded to
  106.     remove the shift/reduce error from rules 1 and 3 below.  The
  107.     problem was, "does the parser see rule variable or rule procedure_stmt
  108.     after a T_ID is seen?"
  109.  
  110. variable: T_ID
  111.         | T_ID '[' expression ']'
  112.         ;
  113.  
  114. procedure_stmt: T_ID
  115.         | T_ID '(' expression ')'
  116.         ;
  117. */
  118.  
  119. expression_list: expression
  120.         | expression_list ',' expression
  121.         | error ','
  122.         ;
  123.  
  124. expression: simple_expr
  125.         | simple_expr relop simple_expr
  126.         ;
  127.  
  128. simple_expr: term
  129.         | sign term
  130.         | simple_expr addop term
  131.         ;
  132.  
  133. term:     factor
  134.         | term mulop factor
  135.         ;
  136.  
  137. factor:   T_ID
  138.         | T_ID '(' expression_list ')'
  139.         | T_ID '[' expression ']'
  140.         | T_NUM
  141.         | T_DIGITS
  142.         | '(' expression ')'
  143.         | '(' ')' { yyerror("empty expressions make no sense"); }
  144.         | T_NOT factor
  145.         | T_TRUE
  146.         | T_FALSE
  147.         ;
  148.  
  149. sign:     '+'
  150.         | '-'
  151.         ;
  152.  
  153. relop:    T_EQ    | T_NOTEQ
  154.         | T_LT    | T_LTEQ
  155.         | T_GTEQ  | T_GT
  156.         ;
  157.  
  158. addop:    sign
  159.         | T_OR
  160.         ;
  161.  
  162. mulop:    '*'
  163.         | '/'
  164.         | T_DIV
  165.         | T_MOD
  166.         | T_AND
  167.         ;
  168.  
  169.